var formSelects = '';
var $;
layui.use(['jquery', 'form', 'formSelects'], function(){
    $ = layui.jquery,
        form = layui.form;
    formSelects = layui.formSelects;
    formSelects.render("train");
    formSelects.render("trainsubject");

    // 基于准备好的dom，初始化echarts实例
    var myChart = echarts.init(document.getElementById('main1'));

    // 指定图表的配置项和数据
    var option = {
        title: {
            //text: '一天用电量分布',
            subtext: '科目分析'
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'cross',
                crossStyle: {
                    color: '#999'
                }
            }
        },
        toolbox: {
            feature: {
                dataView: {show: true, readOnly: false},
                magicType: {show: true, type: ['line']},
                restore: {show: true},
                saveAsImage: {show: true}
            }
        },
        legend: {
            data:[]
        },
        xAxis: [
            {
                type: 'category',
                data: [],
                axisPointer: {
                    type: 'shadow'
                }
            }
        ],
        yAxis: [
            {
                type: 'value',
                name: '成绩',
            }
        ],
        series: [
        ]
    };

    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);

    formSelects.on('trainsubject', function(id, vals, val, isAdd, isDisabled){
        if(isAdd){
            formSelects.config('train', {
                searchUrl: base+'/train/getTrainListsubjectid?subjectid='+val.value
            });
        }else{
            myChart.hideLoading();
            myChart.setOption({
                legend: {
                    data:[]
                },
                xAxis: [
                    {
                        type: 'category',
                        data: [],
                        axisPointer: {
                            type: 'shadow'
                        }
                    }
                ],
                yAxis: [
                    {
                        type: 'value',
                        name: '成绩',
                    }
                ],
                series: [
                    {
                        name:'',
                        type:'line',
                        data:[]
                    }
                ]
            });
            layui.formSelects.value('trainstudent', []);
            layui.formSelects.config('trainstudent', {
                searchUrl: base+'/student/getStudentListTrainid?trainid=0&number='
            });
            layui.formSelects.value('train', []);
            formSelects.config('train', {
                searchUrl: base+'/train/getTrainListsubjectid?subjectid=0'
            });
        }
        return true;
    });


    formSelects.on('train', function(id, vals, val, isAdd, isDisabled){
        if(!isAdd){
            myChart.hideLoading();
            myChart.setOption({
                legend: {
                    data:[]
                },
                xAxis: [
                    {
                        type: 'category',
                        data: [],
                        axisPointer: {
                            type: 'shadow'
                        }
                    }
                ],
                yAxis: [
                    {
                        type: 'value',
                        name: '成绩',
                    }
                ],
                series: [
                    {
                        name:'',
                        type:'line',
                        data:[]
                    }
                ]
            });
            layui.formSelects.value('trainstudent', []);
            layui.formSelects.config('trainstudent', {
                searchUrl: base+'/student/getStudentListTrainid?trainid=0&number='
            });
        }
        return true;
    });



    formSelects.on('trainstudent', function(id, vals, val, isAdd, isDisabled){
        //id:           点击select的id
        //vals:         当前select已选中的值
        //val:          当前select点击的值
        //isAdd:        当前操作选中or取消
        //isDisabled:   当前选项是否是disabled
        var trainarray = formSelects.value('train', 'val');
        if(trainarray.length == 0){
            layer.alert('请选择批次', function(index){
                layer.close(index);
            });
            return false;
        }
        //如果return false, 那么将取消本次操作\
        if(isAdd){
            // 异步加载数据
            $.get(base+'/analyst/getAnalyst?trainids='+formSelects.value('train', 'val')+"&subjectid="+formSelects.value('trainsubject', 'val')+"&studentid="+val.value).done(function (data) {
                myChart.hideLoading();
                // 填入数据
                myChart.setOption({
                    legend: {
                        data:[data.subjectname]
                    },
                    xAxis: [
                        {
                            type: 'category',
                            data: data.xAxisData,
                            axisPointer: {
                                type: 'shadow'
                            }
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            name: '成绩',
                            min:data.min,
                            max:data.max,
                            interval:data.interval
                        }
                    ],
                    series: [
                        {
                            name:data.subjectname,
                            type:'line',
                            data:data.seriesData
                        }
                    ]
                });
            });
        }else{
            myChart.hideLoading();
            myChart.setOption({
                legend: {
                    data:[]
                },
                xAxis: [
                    {
                        type: 'category',
                        data: [],
                        axisPointer: {
                            type: 'shadow'
                        }
                    }
                ],
                yAxis: [
                    {
                        type: 'value',
                        name: '成绩',
                    }
                ],
                series: [
                    {
                        name:'',
                        type:'line',
                        data:[]
                    }
                ]
            });
        }
        // console.log(val);
        return true;
    });
});

function getkemu(){
    var trainstudentarray = formSelects.value('train', 'val');
    if(trainstudentarray.length == 0){
        layer.alert('请选择批次', function(index){
            layer.close(index);
        });
        return false;
    }
    var number = $("#number").val();
    formSelects.config('trainstudent', {
        searchUrl: base+'/student/getStudentListTrainid?trainid='+trainstudentarray+'&number='+number
    });
    console.log(formSelects.value('train', 'val'));
}